const ossconfig = require('../release/config.js')
const config = {
  destRoot: 'zhuanti/testnode'
}
const OSS = require('ali-oss')
const client = new OSS(ossconfig)
const path = require('path')
const fs = require('fs')
const glob = require('glob')
console.log(`helloworld`)
console.log(path.resolve('./1.js'))

let obj = {
  a: 1,
  b: 2
}
const uploadFile = (key, filepath) => {
  let allkey = path.join(config.destRoot, key)
  allkey = allkey.replace(/\\/g, '/')
  console.log(allkey)
  //client.put(allkey, filepath).then((res) => {
  //console.log(filepath);
  //});
}
//glob上传文件

//let files = glob.sync(path.resolve(__dirname, '../dist/**/*.*'))
//console.log(files)
//files.forEach(file => {
//let relativePath = path.relative(path.resolve(__dirname,'../dist/'),file);
//relativePath = relativePath.replace(/\\/g,'/');
//let key = relativePath;
//let filepath = file;
//uploadFile(key,filepath);
//});

//fs.writeFileSync(path.resolve(__dirname,'../','1.json'),JSON.stringify(obj),'utf-8');

//const http = require('http');

//const server = http.createServer((req, res) => {
//res.setHeader('Content-Type', 'text/html');
//res.setHeader('X-Foo', 'bar');
////res.writeHead(200, { 'Content-Type': 'text/plain' });
////res.end('ok');
//});
//server.on('clientError', (err, socket) => {
//socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
//});
//server.on('request',function(message,res) {
//res.write('999');
//res.end();
//});
//server.listen(8000);

//console.time('start')
//console.log(`这里执行了一些东西`)
//console.timeEnd('start')

//let str = 'a\\bb\\bb'
//let arr = str.split(path.sep)
//console.log(arr)
//

let files = glob.sync('**/*.*', {
  cwd: path.resolve(__dirname, './files/')
})

let data = new Map()
files.forEach((res) => {
  let basename = path.basename(res)
  let [id] = basename.split('.')
  if (!data.has(id)) {
    data.set(id, {
      id
    })
  }
  if (/png|jpg/.test(res)) {
    let item = data.get(id)
    item.pic = res
  }
  if (/mp4/.test(res)) {
    let item = data.get(id)
    item.video = res
  }
})

let text = JSON.stringify([...data.values()])
console.log(text)
//fs.writeFileSync(path.resolve(__dirname, 'file.json'), text, 'utf-8')
